﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using ThousandFunClient.model;

using System.Configuration;
//using ThousandFunClient.model;
using ThousandFunClient.Util;
//using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;
using System.Media;
using System.Runtime.InteropServices;
using System.Data.SqlClient;

using System.Diagnostics;
using System.IO;

namespace ThousandFunClient
{
    public partial class Fm_XmlImport : Form
    {
        public Fm_XmlImport()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result = false;
            try
            {
                //IDataParameter[] parameters = new SqlParameter[]{
                //    new SqlParameter("@TransactionID_SP",obj.TransactionID_SP),
                //    new SqlParameter("@TransactionID_Ext",obj.TransactionID_Ext),
                //    new SqlParameter("@CustID",obj.CustID),
                //    new SqlParameter("@SPID",obj.SPID),
                //    new SqlParameter("@OptionType",obj.OptionType),
                //    new SqlParameter("@ReqTime",obj.ReqTime),
                //    new SqlParameter("@CompleteTime",obj.CompleteTime),
                //    new SqlParameter("@BestPayAccount",obj.BestPayAccount),
                //    new SqlParameter("@PW",obj.PW),
                //    new SqlParameter("@Status",obj.Status),
                //    new SqlParameter("@IsDeleted",obj.IsDeleted)
                //};

                SqlCommand cmd = new SqlCommand(Insert_OPResult_Sql);
                cmd.CommandType = CommandType.Text;
                //cmd.Parameters.AddRange(parameters);
                Constants.syslogger.Error(DBUtility.ThousandFunConStr);
                result = DBUtility.Execute(cmd, DBUtility.ThousandFunConStr);
            }
            catch (Exception ex) 
            {
                Constants.syslogger.Error("存入数据库报错",ex);
            }



        }

        //INSERT INTO [ToolsNetDB].[dbo].[OPResult] ([SystemType] ,[SystemNumber] ,[StationNumber] ,[SpindleNumber] ,[ProgramNumber] ,[ResultTime] ,[RundownCounter] ,[VIN] ,[ToolSerialNo] ,[OverallStatus] ,[FinalTorque] ,[FinalTorqueStatus] ,[FinalAngle] ,[FinalAngleStatus] ,[TimeStatus] ,[TorqueLimitLow] ,[TorqueLimitHigh] ,[AngleLimitLow] ,[AngleLimitHigh]) VALUES (<SystemType, smallint,> ,<SystemNumber, smallint,> ,<StationNumber, smallint,> ,<SpindleNumber, smallint,> ,<ProgramNumber, smallint,> ,<ResultTime, datetime,> ,<RundownCounter, int,> ,<VIN, varchar(25),> ,<ToolSerialNo, varchar(25),> ,<OverallStatus, smallint,> ,<FinalTorque, real(24,0),> ,<FinalTorqueStatus, smallint,> ,<FinalAngle, real(24,0),> ,<FinalAngleStatus, smallint,> ,<TimeStatus, smallint,> ,<TorqueLimitLow, real(24,0),> ,<TorqueLimitHigh, real(24,0),> ,<AngleLimitLow, real(24,0),> ,<AngleLimitHigh, real(24,0),>) 
        private String Insert_OPResult_Sql = String.Format("INSERT INTO OPResult (SystemType ,SystemNumber ,StationNumber ,SpindleNumber ,ProgramNumber ,ResultTime ,RundownCounter ,VIN ,ToolSerialNo ,OverallStatus ,FinalTorque ,FinalTorqueStatus ,FinalAngle ,FinalAngleStatus ,TimeStatus ,TorqueLimitLow ,TorqueLimitHigh ,AngleLimitLow ,AngleLimitHigh) VALUES (1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1)");
        private String Test_OPResult_Sql = String.Format("select 1 from dbo.OPResult");
        //private String Insert_OPResult_Sql = String.Format("INSERT INTO [ToolsNetDB].[dbo].[OPResult] ([SystemType] ,[SystemNumber] ,[StationNumber] ,[SpindleNumber] ,[ProgramNumber] ,[ResultTime] ,[RundownCounter] ,[VIN] ,[ToolSerialNo] ,[OverallStatus] ,[FinalTorque] ,[FinalTorqueStatus] ,[FinalAngle] ,[FinalAngleStatus] ,[TimeStatus] ,[TorqueLimitLow] ,[TorqueLimitHigh] ,[AngleLimitLow] ,[AngleLimitHigh]) VALUES (1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1)");
        private String Insert_Sql = String.Format("Insert into AccountBindingRecord(TransactionID_SP,TransactionID_Ext,CustID,SPID,OptionType,ReqTime,CompleteTime,BestPayAccount,PW,Status,IsDeleted) values(@TransactionID_SP,@TransactionID_Ext,@CustID,@SPID,@OptionType,@ReqTime,@CompleteTime,@BestPayAccount,@PW,@Status,@IsDeleted)");
        private String Update_Sql = String.Format("Update AccountBindingRecord set TransactionID_SP = @TransactionID_SP,TransactionID_Ext = @TransactionID_Ext,CustID = @CustID,SPID = @SPID,OptionType = @OptionType,ReqTime = @ReqTime,CompleteTime = @CompleteTime,BestPayAccount = @BestPayAccount,PW = @PW,Status= @Status,IsDeleted = @IsDeleted where ID = @ID");
        private String Select_Sql = String.Format("Select ID,TransactionID_SP,TransactionID_Ext,CustID,SPID,OptionType,ReqTime,CompleteTime,BestPayAccount,PW,Status,IsDeleted from AccountBindingRecord ");

        private void button2_Click(object sender, EventArgs e)
        {
            bool result = false;
            try
            {
                SqlCommand cmd = new SqlCommand(Test_OPResult_Sql);
                cmd.CommandType = CommandType.Text;
                Constants.syslogger.Error(DBUtility.ThousandFunConStr);
                result = DBUtility.Execute(cmd, DBUtility.ThousandFunConStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接数据库报错:" + ex.ToString());
                Constants.syslogger.Error("连接数据库报错", ex);
            }
        }

        private void Fm_XmlImport_Load(object sender, EventArgs e)
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = xmlimportpath;
            /* Watch for changes in LastAccess and LastWrite times, and 
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = "*.xml";

            // Add event handlers.
            //watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            //watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;



            //初始化
            //XmlDocument xd = new XmlDocument();//定义一个xmldocument对象
            //string xmlpath = XMLImportConfigAddress;//加载xml文件
            //xd.Load(@xmlpath);
            //XmlElement tree = xd.DocumentElement;
            //XmlNodeList xmllist = tree.SelectNodes("/DynaPower/XMLImport/DataMatch");
            //foreach (XmlNode node in xmllist)
            //{
            //    //htmatchdata.Add(node.Attributes["databasecolumn"].Value,node.Attributes["databasecolumn"].Value,
            //    XMLImportConfig xmlImportConfig = new XMLImportConfig(node);

            //    htmatchdata.Add(xmlImportConfig.Databasecolumn, xmlImportConfig);
            //}

        }

        // Define the event handlers.
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);

            //File.Move(e.FullPath, removepathfinal + e.Name);
            //runpicture++;
            //if (runpicture >= filenum)
            //{
            //    runpicture = 0;
            //}

           
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            MessageBox.Show(e.OldFullPath + "----" + e.FullPath);
        }



        private static string xmlimportpath = System.Configuration.ConfigurationSettings.AppSettings["xmlimportpath"];
        //private static string imageconfigpath = System.Configuration.ConfigurationSettings.AppSettings["imageconfigpath"];
        //private static string xmlimportremovepath = System.Configuration.ConfigurationSettings.AppSettings["xmlimportremovepath"];
        private Hashtable htmatchdata = new Hashtable();

        private void button3_Click(object sender, EventArgs e)
        {
            //XElement xe = XElement.Load("c:\\testexample.xml");
            //xe.
            htmatchdata.Add("/DATAPACKET/METADATA/HEADER/FIELD[1]", "1");


            XmlDocument xd = new XmlDocument();//定义一个xmldocument对象
            string xmlpath = "c:\\testexample.xml";//加载xml文件
            xd.Load(@xmlpath);
            XmlElement tree = xd.DocumentElement;
            //XmlNode xn1 = tree.SelectSingleNode();
            XmlNode xn2 = tree.SelectSingleNode("/DATAPACKET/METADATA/HEADER/FIELD[2]");
            XmlNode xn3 = tree.SelectSingleNode("/DATAPACKET/METADATA/HEADER//FIELD[@attrname='Anlagentyp']");

            XmlNodeList xn5 = tree.SelectNodes("/DATAPACKET/METADATA/HEADER//FIELD[@attrname='Anlagentyp']");

            XmlNode xn4 = tree.SelectSingleNode("/HEADER/FIELD[1]");


        }
        public static string XMLImportConfigAddress = System.AppDomain.CurrentDomain.BaseDirectory + "XMLImportConfig.xml";
        public static string Insert_OPResult_SQL = String.Format("INSERT INTO OPResult (SystemType ,SystemNumber ,StationNumber ,SpindleNumber ,ProgramNumber ,ResultTime ,RundownCounter ,VIN ,ToolSerialNo ,OverallStatus ,FinalTorque ,FinalTorqueStatus ,FinalAngle ,FinalAngleStatus ,TimeStatus ,TorqueLimitLow ,TorqueLimitHigh ,AngleLimitLow ,AngleLimitHigh) VALUES (1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1)");
        
        private void button4_Click(object sender, EventArgs e)
        {

            foreach (System.Collections.DictionaryEntry item in htmatchdata)
            {
                item.Key.ToString();//键

                item.Value.ToString();//值

                //这里已经取到当前的键、值对了，怎么输出和存储就看自己的了　

            }

            bool result = false;
            //try
            //{
            //    IDataParameter[] parameters = new SqlParameter[]{
            //        new SqlParameter("@TransactionID_SP",obj.TransactionID_SP),
            //        new SqlParameter("@TransactionID_Ext",obj.TransactionID_Ext),
            //        new SqlParameter("@CustID",obj.CustID),
            //        new SqlParameter("@SPID",obj.SPID),
            //        new SqlParameter("@OptionType",obj.OptionType),
            //        new SqlParameter("@ReqTime",obj.ReqTime),
            //        new SqlParameter("@CompleteTime",obj.CompleteTime),
            //        new SqlParameter("@BestPayAccount",obj.BestPayAccount),
            //        new SqlParameter("@PW",obj.PW),
            //        new SqlParameter("@Status",obj.Status),
            //        new SqlParameter("@IsDeleted",obj.IsDeleted)
            //    };

            //    SqlCommand cmd = new SqlCommand(Insert_Sql);
            //    cmd.CommandType = CommandType.Text;
            //    cmd.Parameters.AddRange(parameters);

            //    result = DBUtility.Execute(cmd, DBUtility.BestToneCenterConStr);
            //}
            //catch { }

            //return result;

        }
    }
}
